package com.yhans.serviceImpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yhans.constant.MessageConstant;
import com.yhans.dao.CheckItemDao;
import com.yhans.entity.PageResult;
import com.yhans.entity.QueryPageBean;
import com.yhans.entity.Result;
import com.yhans.pojo.CheckItem;
import com.yhans.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * 检查项服务
 */
@Service(interfaceClass = CheckItemService.class)
@Transactional
public class CheckItemServiceImpl implements CheckItemService {

    @Autowired
    private CheckItemDao checkItemDao;


    public void add(CheckItem checkItem) {

        checkItemDao.add(checkItem);
    }

    public PageResult pageQuery(QueryPageBean queryPageBean) {

        //获取条件
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        if (queryString!=null&&queryString.length()!=0){
            currentPage=1;
        }

        System.out.print(currentPage);
        System.out.print(pageSize);
        System.out.print(queryString);

        //完成分页查询,基于mybatis框架提供的分页助手
        PageHelper.startPage(currentPage,pageSize);

        Page<CheckItem> page = checkItemDao.selectByCondition(queryString);

        long total = page.getTotal();

        List<CheckItem> result = page.getResult();

        return new PageResult(total,result);
    }

    public void deleteById(Integer id) {
        //判断检查项是否关联到检查组
        long countByCheckItemId = checkItemDao.findCountByCheckItemId(id);
        if (countByCheckItemId>0){
           new RuntimeException();
        }
        checkItemDao.deleteById(id);
    }

    public CheckItem findById(Integer id) {

            return checkItemDao.findById(id);

    }

    public void edit(CheckItem checkItem) {

        checkItemDao.edit(checkItem);

    }

    public List<CheckItem> findAll() {

        return checkItemDao.findAll();
    }


}
